package pers.liu.yibandemo.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import pers.liu.yibandemo.entity.Parent;
import pers.liu.yibandemo.service.ParentService;

import java.util.List;

@RestController
@RequestMapping("/parents")
public class ParentController {

    @Autowired
    private ParentService parentService;

    @PostMapping("/add")
    public ResponseEntity<String> addParent(@RequestBody Parent parent) {
            parentService.addParent(parent);
        return ResponseEntity.ok("Parent added successfully.");
    }

    @GetMapping("/{id}")
    public ResponseEntity<Parent> getParentById(@PathVariable Long id) {
        Parent parent = parentService.getParentById(id);
        if (parent != null) {
            return ResponseEntity.ok(parent);
        } else {
            return ResponseEntity.notFound().build();
        }
    }

    @GetMapping("/all")
    public ResponseEntity<List<Parent>> getAllParents() {
        List<Parent> parents = parentService.getAllParents();
        return ResponseEntity.ok(parents);
    }

    @PutMapping("/update")
    public ResponseEntity<String> updateParent(@RequestBody Parent parent) {
        parentService.updateParent(parent);
        return ResponseEntity.ok("Parent updated successfully.");
    }

    @DeleteMapping("/delete/{id}")
    public ResponseEntity<String> deleteParent(@PathVariable Long id) {
        parentService.deleteParent(id);
        return ResponseEntity.ok("Parent deleted successfully.");
    }
    @GetMapping("/page")
    public ResponseEntity<IPage<Parent>> getParentsByPage(
            @RequestParam(defaultValue = "1") int pageNum,
            @RequestParam(defaultValue = "10") int pageSize) {
        IPage<Parent> parents = parentService.getParentsByPage(pageNum, pageSize);
        return ResponseEntity.ok(parents);
    }
    @GetMapping("/json")
    public String getAllId(){
        return parentService.getAllParentIdsAsJson();
    }

}
